package vn.appmobi.comictp;

import java.util.ArrayList;
import vn.gq.tym.scr.ImageViewScr;
import vn.gq.tym.widget.OnViewPagerDisablePagingListener;
import vn.gq.tym.widget.PopupWindow;
import vn.gq.tym.widget.SwipeyTabs;
import vn.gq.tym.widget.SwipeyTabsAdapter;
import vn.gq.tym.widget.ViewPager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.startapp.android.publish.StartAppAd;


public class ReaderActivity extends FragmentActivity implements OnViewPagerDisablePagingListener, OnDoubleTapListener, OnClickListener {

	public static final String EXTRA_IMAGES_LIST = "EXTRA_IMAGES_LIST";
	public static final String EXTRA_POSITION = "EXTRA_POSITION";
	private static final String STATE_POSITION = "STATE_POSITION";
	private static final int DEFAULT_DELAY_TIME = 25000;
	
	private ArrayList<String> mImageList;
	private int mCurrentPos = 0;
	private SwipeyTabs mTabs;
	private ViewPager mViewPager;
	private ImageLoader imagerLoader = ImageLoader.getInstance();
	private PopupWindow mPopupWindow;
	private LinearLayout mLayoutContainer;
	
	private Button btnOk, btnFirst, btnLast;
	private EditText editText;
	private Handler mHandler = new Handler();
	private StartAppAd startAppAd = new StartAppAd(this);
	private Runnable showAdRun = new Runnable() {
		
		@Override
		public void run() {
			if (!startAppAd.isReady()) {
				startAppAd.loadAd();
			}
			startAppAd.showAd();
			mHandler.removeCallbacks(showAdRun);
			mHandler.postDelayed(showAdRun, DEFAULT_DELAY_TIME);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		StartAppAd.init(this, AppConfig.STARTAPP_DEV_ID, AppConfig.STARTAPP_APP_ID);
		setContentView(R.layout.activity_reader);
		mLayoutContainer = (LinearLayout) findViewById(R.id.reader_container);
		mImageList = getIntent().getExtras().getStringArrayList(EXTRA_IMAGES_LIST);
		mCurrentPos = getIntent().getExtras().getInt(EXTRA_POSITION);
		if (savedInstanceState != null) {
			mCurrentPos = savedInstanceState.getInt(STATE_POSITION);
		}
		ImagePagerAdapter adapter = new ImagePagerAdapter(getSupportFragmentManager(), mImageList);
		mTabs = (SwipeyTabs) findViewById(R.id.swipeytabs);
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(adapter);
		mTabs.setAdapter(adapter);
		mViewPager.setOnPageChangeListener(mTabs);
		mViewPager.setCurrentItem(mCurrentPos);
		initializePopupWindow();
		Toast.makeText(this, getString(R.string.have_a_nice), Toast.LENGTH_SHORT).show();
		
		mHandler.postDelayed(showAdRun, DEFAULT_DELAY_TIME);
		startAppAd.loadAd();
	}
	
	public void initializePopupWindow() {
		mPopupWindow = new PopupWindow(this);
		ViewGroup popupView = (ViewGroup) getLayoutInflater().inflate(R.layout.popup, null, false);
		mPopupWindow.setContentView(popupView);
		btnFirst = (Button) popupView.findViewById(R.id.first);
		btnFirst.setOnClickListener(this);
		btnLast = (Button) popupView.findViewById(R.id.last);
		btnLast.setOnClickListener(this);
		btnOk = (Button) popupView.findViewById(R.id.enter);
		btnOk.setOnClickListener(this);
		editText = (EditText) popupView.findViewById(R.id.page_num);
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putInt(STATE_POSITION, mCurrentPos);
		super.onSaveInstanceState(outState);
	}

	public static void startActivity(Context context, ArrayList<String> imageList, int position) {
		Intent intent = new Intent(context, ReaderActivity.class);
		intent.putExtra(EXTRA_IMAGES_LIST, imageList);
		intent.putExtra(EXTRA_POSITION, position);
		context.startActivity(intent);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}

	private class ImagePagerAdapter extends FragmentStatePagerAdapter implements SwipeyTabsAdapter {
		
		
		private ArrayList<String> images;
		private LayoutInflater mInflater;
		
		public ImagePagerAdapter(FragmentManager fm, ArrayList<String> images) {
			super(fm);
			this.images = images;
			mInflater = getLayoutInflater();
		}

		@Override
		public Fragment getItem(int position) {
			return ImageViewScr.newInstance(images.get(position));
		}

		@Override
		public int getCount() {
			return images.size();
		}

		@Override
		public TextView getTab(final int position, SwipeyTabs root) {
			TextView view = (TextView) mInflater.inflate(
					R.layout.swipey_tab_indicator, root, false);
			view.setText(String.format("%d/%d", position + 1, getCount()));
			view.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					mViewPager.setCurrentItem(position);
				}
			});
			
			return view;
		}
	}

	@Override
	public void onDisable(boolean val) {
		if (val) {
			mViewPager.setPagingEnable(false);
		} else {
			mViewPager.setPagingEnable(true);
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		imagerLoader.stop();
		mHandler.removeCallbacks(showAdRun);
	}

	@Override
	public void onBackPressed() {
		if (mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
		}
		Bookmark.savePosition(this, mViewPager.getCurrentItem());
		super.onBackPressed();
	}

	@Override
	public boolean onSingleTapConfirmed(MotionEvent e) {
		
		if (!mPopupWindow.isShowing()) {
			mPopupWindow.showAtLocation(mLayoutContainer, Gravity.CENTER, 0, 0);
		} else {
			mPopupWindow.dismiss();
		}
		
		return false;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		
	}

	@Override
	public boolean onDoubleTap(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onDoubleTapEvent(MotionEvent e) {
		return false;
	}

	@Override
	public void onClick(View v) {
		if (v.equals(btnFirst)) {
			if (mViewPager.getCurrentItem() > 0) mViewPager.setCurrentItem(0);
		} else if (v.equals(btnLast)) {
			if (mViewPager.getCurrentItem() < mImageList.size() - 1) mViewPager.setCurrentItem(mImageList.size() - 1);
		} else if (v.equals(btnOk)) {
			try {
				Integer page = Integer.parseInt(editText.getText().toString());
				page--;
				if (page >=0 && (page <=mImageList.size()-1)) {
					if (page != mViewPager.getCurrentItem()) {
						mViewPager.setCurrentItem(page);
					}
				}
			} catch(Exception e) {
			}
		}
		if (mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
		}
	}
}
